package com.tledu.springboot04.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tledu.springboot04.core.model.AjaxResult;
import com.tledu.springboot04.domain.User;
import com.tledu.springboot04.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final IUserService userService;

    @GetMapping()
    public String index() {
        return "user/list";
    }

    @GetMapping("/page")
    @ResponseBody
    public AjaxResult<PageInfo<User>> page(Integer pageNum, Integer pageSize, User user) {
        if (pageNum == null) pageNum = 1;
        if (pageSize == null) pageNum = 5;
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userService.list(user);
        PageInfo<User> pageInfo = PageInfo.of(list);
        return new AjaxResult<>(pageInfo);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public AjaxResult<Void> delete(@PathVariable Integer id) {
        userService.delete(id);
        return new AjaxResult<>();
    }
}
